﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DependenciesFeatureReceiver.cs" company="Lambros Vasiliou">
//   © 2013
// </copyright>
// <summary>
//   Content Type Data Item.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace LV.EnhancedNew.FeatureReceivers
{
    using System;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Reflection;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    
    /// <summary>
    /// Deploys Dependencies of the Enhanced New Dialog
    /// </summary>
    public class DependenciesFeatureReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Adds the assembly to the web.config of the site.
        /// </summary>
        /// <param name="properties">Feature parameters</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebService service = SPWebService.ContentService;

            var myModification = new SPWebConfigModification
            {
                Path = "system.Web/compilation/assemblies",
                Name = string.Format(CultureInfo.InvariantCulture, "add [@assembly=\"{0}\"]", Assembly.GetExecutingAssembly().FullName),
                Sequence = 0,
                Owner = Constants.WebConfigModificationOwner,
                Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                Value = string.Format(CultureInfo.InvariantCulture, "<add @assembly=\"{0}\" />", Assembly.GetExecutingAssembly().FullName)
            };
            service.WebConfigModifications.Add(myModification);

            service.Update();
            service.ApplyWebConfigModifications();
        }

        /// <summary>
        /// Removes the assembly to the web.config of the site.
        /// </summary>
        /// <param name="properties">Feature parameters</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            SPWebConfigModification configModFound = null;
            var webApplication = properties.Feature.Parent as SPWebApplication;
            if (webApplication != null)
            {

                Collection<SPWebConfigModification> modsCollection = webApplication.WebConfigModifications;

                int modsCount1 = modsCollection.Count;

                for (int i = modsCount1 - 1; i > -1; i--)
                {
                    if (modsCollection[i].Owner.Equals(Constants.WebConfigModificationOwner,
                        StringComparison.OrdinalIgnoreCase))
                    {
                        configModFound = modsCollection[i];
                    }
                }

                modsCollection.Remove(configModFound);
                webApplication.Update();

                webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            }
        }
    }
}
